﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace Avivo.Tools.Sprite
{
	public partial class SpriteGrid : List<SpriteRow>
	{
		#region Serialization
		public static SpriteGrid Create(XDocument element)
		{
			SpriteGrid result = null;
			if (element.Root.Name == "SpriteGrid")
			{
				result = new SpriteGrid();

				foreach (var item in element.Root.Elements())
				{
					var row = SpriteRow.Create(item);
					if (row != null)
					{
						result.Add(row);
					}
				}
			}
			return result;
		}

		public static SpriteGrid Create(XElement element)
		{
			SpriteGrid result = null;
			if (element.Name == "SpriteGrid")
			{
				result = new SpriteGrid();

				foreach (var item in element.Elements())
				{
					var row = SpriteRow.Create(item);
					if (row != null)
					{
						result.Add(row);
					}
				}
			}
			return result;
		}

		public XElement ToXElement()
		{
			XElement element = new XElement("SpriteGrid");
			foreach (var item in this)
			{
				element.Add(item.ToXElement());
			}
			return element;
		}
		#endregion
	}
}
